//
//  AppDelegate.m
//  OFootball
//
//  Created by 上官千城 on 15/9/21.
//  Copyright (c) 2015年 上官千城. All rights reserved.
//

#import "AppDelegate.h"
#import "ODbManager.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


-(void) loadHttpSession
{
    //TODO:从数据库中加载
    self.httpSessionDic = [NSMutableDictionary dictionary];
    
}



-(void) layout
{
    OConsultSegmentController *consult = [[OConsultSegmentController alloc]init];
    OBaseNavViewController *consultNav = [[OBaseNavViewController alloc]initWithRootViewController:consult];
    consultNav.tabBarItem.image = [[UIImage imageNamed:@"manpage_37off@2x"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    consultNav.tabBarItem.selectedImage = [[UIImage imageNamed:@"manpage_37on@2x"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    consultNav.navigationBar.translucent = NO;

    
    OCircleViewController *circle = [[OCircleViewController alloc]init];
    OBaseNavViewController *circleNav = [[OBaseNavViewController alloc]initWithRootViewController:circle];
    circleNav.tabBarItem.image = [[UIImage imageNamed:@"manpage_42off@2x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    circleNav.tabBarItem.selectedImage = [[UIImage imageNamed:@"manpage_42on@2x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    circleNav.navigationBar.translucent = NO;
    
    OFriendViewController *friend = [[OFriendViewController alloc]init];
    OBaseNavViewController *friendNav = [[OBaseNavViewController alloc]initWithRootViewController:friend];
    friendNav.tabBarItem.image = [[UIImage imageNamed:@"manpage_40off@2x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    friendNav.tabBarItem.selectedImage = [[UIImage imageNamed:@"manpage_40on@2x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    friendNav.navigationBar.translucent = NO;
    
    
    OSettingViewController *setting = [[OSettingViewController alloc]init];
    OBaseNavViewController *settingNav = [[OBaseNavViewController alloc]initWithRootViewController:setting];
    settingNav.tabBarItem.image = [[UIImage imageNamed:@"manpage_44off@2x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    settingNav.tabBarItem.selectedImage = [[UIImage imageNamed:@"manpage_44on@2x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    settingNav.navigationBar.translucent = NO;
    
    
    
    
    
    //    nightTabBarViewController *tabBarC1 = [[nightTabBarViewController alloc] init];
    UITabBarController *tabBarC = [[UITabBarController alloc]init];
    tabBarC.viewControllers = [NSArray arrayWithObjects:consultNav,circleNav,friendNav,settingNav, nil];
    tabBarC.tabBar.translucent = NO;
    tabBarC.tabBar.barTintColor = [UIColor blackColor];
    
    //    tabBarC1.myDelegate = self;
    
    
    
    ODrawerViewController *leftMenuViewController = [[ODrawerViewController alloc] init];
    self.sideMenuViewController = [[RESideMenu alloc] initWithContentViewController:tabBarC leftMenuViewController:leftMenuViewController rightMenuViewController:nil];
    self.sideMenuViewController.backgroundImage = [UIImage imageNamed:@"Stars"];
    self.sideMenuViewController.menuPreferredStatusBarStyle = 1;
    self.sideMenuViewController.delegate = self;
    self.sideMenuViewController.contentViewShadowColor = [UIColor blackColor];
    self.sideMenuViewController.contentViewShadowOffset = CGSizeMake(0, 0);
    self.sideMenuViewController.contentViewShadowOpacity = 0.6;
    self.sideMenuViewController.contentViewShadowRadius = 12;
    self.sideMenuViewController.contentViewShadowEnabled = YES;
    
    self.window.rootViewController = self.sideMenuViewController;
    
    
    
    //友盟分享
    [UMSocialData setAppKey:@"55b055cc67e58eb761006f7a"];
    
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    if ([@"on" isEqualToString:[userDefaults objectForKey:@"isNight"]]) {
        self.window.alpha = 0.7;
    } else {
        self.window.alpha = 1;
    }
}

//进入应用时判定如何启动
-(void) startRootViewController
{
    if (!self.userinfo) {
        NSLog(@"首次启动");
                OLoginViewController *vc = [[OLoginViewController alloc] init];
                vc.loginDelegate = self;
                self.window.rootViewController = vc;
    }
    else {
        NSLog(@"非首次启动");
        //TODO:实现AppEnter
    }
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    [ODbManager initializeDbManager:[NSArray arrayWithObjects:SESSION_DOMAIN,USER_INFO, nil]];
    self.userinfo = [OUserInfo loadInfo];
    NSLog(@"userInformation = = %@",self.userinfo);
    [self loadHttpSession];
    
    [self unRegisterNotification];
    [self registerNotification];
    
    [self layout];
    [[OImManager shareManager] initManager:launchOptions];
    [self startRootViewController];
    
    
    return YES;
}

//协议 方法  设置根式图
-(void)setRootViewControllerLog
{
    self.window.rootViewController = self.sideMenuViewController;
}

-(void)nightChange {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    if ([@"on" isEqualToString:[userDefaults objectForKey:@"isNight"]]) {
        
        self.window.alpha = 0.7;
    } else {
        
        self.window.alpha = 1;
        
    }
    
    NSLog(@"33333333");
    
    
    
    
}

//退出app
- (void)exitApplication {
    
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    UIWindow *window = app.window;
    
    [UIView animateWithDuration:1.0f animations:^{
        window.alpha = 0;
        window.frame = CGRectMake(0, window.bounds.size.width, 0, 0);
    } completion:^(BOOL finished) {
        exit(0);
    }];
    //exit(0);
    
}


- (void)applicationWillResignActive:(UIApplication *)application {

        NSLog(@"\n ===> 程序暂行 !");
}

- (void)applicationDidEnterBackground:(UIApplication *)application {

    NSLog(@"HOME");
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshData" object:nil];
    
    
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    NSLog(@"\n ===> 程序进入前台 !");

}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    NSLog(@"\n ===> 程序重新激活 !");
}

- (void)applicationWillTerminate:(UIApplication *)application {
        NSLog(@"\n ===> 程序意外暂行 !");   
}

-(void) registerNotification
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(loginStateChange:)
                                                 name:KNOTIFICATION_LOGINCHANGE
                                               object:nil];
    
}

-(void) unRegisterNotification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


#pragma mark - private
//登陆状态改变
-(void)loginStateChange:(NSNotification *)notification
{
    BOOL isAutoLogin = [[[EaseMob sharedInstance] chatManager] isAutoLoginEnabled];
    BOOL loginSuccess = [notification.object boolValue];
    
    if (isAutoLogin || loginSuccess) {
        [[ApplyViewController shareController] loadDataSourceFromLocalDB];
        
        if (self.mainVC == nil) {
            self.mainVC = [[MainViewController alloc] init];
            [self.mainVC networkChanged:_connectionState];
            
            self.window.rootViewController = self.sideMenuViewController;
        }
    }
    else{//登陆失败加载登陆页面控制器
        [OUserInfo deleteInfo];
        [OUserInfo setInfo:nil];
        self.mainVC = nil;
        OLoginViewController *loginController = [[OLoginViewController alloc] init];
        self.window.rootViewController = loginController;
    }
    
    
    /*TODO:
     UINavigationController *nav = nil;
     
     BOOL isAutoLogin = [[[EaseMob sharedInstance] chatManager] isAutoLoginEnabled];
     BOOL loginSuccess = [notification.object boolValue];
     
     if (isAutoLogin || loginSuccess) {//登陆成功加载主窗口控制器
     //加载申请通知的数据
     [[ApplyViewController shareController] loadDataSourceFromLocalDB];
     if (_mainController == nil) {
     _mainController = [[MainViewController alloc] init];
     [_mainController networkChanged:_connectionState];
     nav = [[UINavigationController alloc] initWithRootViewController:_mainController];
     }else{
     nav  = _mainController.navigationController;
     }
     }else{//登陆失败加载登陆页面控制器
     _mainController = nil;
     LoginViewController *loginController = [[LoginViewController alloc] init];
     nav = [[UINavigationController alloc] initWithRootViewController:loginController];
     loginController.title = NSLocalizedString(@"AppName", @"JuDuo");
     }
     
     //设置7.0以下的导航栏
     if ([UIDevice currentDevice].systemVersion.floatValue < 7.0){
     nav.navigationBar.barStyle = UIBarStyleDefault;
     [nav.navigationBar setBackgroundImage:[UIImage imageNamed:@"titleBar"]
     forBarMetrics:UIBarMetricsDefault];
     
     [nav.navigationBar.layer setMasksToBounds:YES];
     }
     
     self.window.rootViewController = nav;
     
     [nav setNavigationBarHidden:YES];
     [nav setNavigationBarHidden:NO];*/
}

@end
